<template>
  <div class="custom-manage">
    <el-card>
      <h3>
        房间类型<el-button type="primary" icon="el-icon-circle-plus-outline" size="small" circle
          @click="dialogFormVisible = true"></el-button>
      </h3>
      <el-table style="width: 100%" :data="roomTypeList.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
        border :cell-style="cellStyle">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" :index="indexMethod" width="80"></el-table-column>
        <el-table-column prop="roomType" label="房间类型" width="80"> </el-table-column>
        <el-table-column prop="roomUrl" label="房间图片" width="130">
          <template slot-scope="scope">
            <img :src="scope.row.roomUrl" alt="" style="width: 100%;height: 50px;">
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="80"> </el-table-column>
        <el-table-column prop="description" label="描述" width="150"> </el-table-column>
        <el-table-column prop="isAdvice" label="推荐" width="80"></el-table-column>
        <el-table-column prop="typeExplain" label="详细说明" width="400"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="primary" size="small" icon="el-icon-edit" circle
              @click="editType(scope.row.roomTypeId)"></el-button>
            <el-button type="danger" size="small" circle icon="el-icon-delete"
              @click="deleteType(scope.row.roomTypeId)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="请填写房间类型" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="房间类型" :label-width="formLabelWidth">
          <el-input v-model="form.roomType" autocomplete="off" placeholder="请输入类型" value="this.form.vipType"></el-input>
        </el-form-item>
        <el-form-item label="价格" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off" placeholder="请输入价格" type="number"
            value="this.form.price"></el-input>
        </el-form-item>
        <el-form-item label="房间描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off" placeholder="请输入描述" type="number"
            value="this.form.discountRate"></el-input>
        </el-form-item>
        <el-form-item label="房间推荐" :label-width="formLabelWidth">
          <el-radio v-model="form.isAdvice" label="推荐">推荐</el-radio>
          <el-radio v-model="form.isAdvice" label="不推荐">不推荐</el-radio>
        </el-form-item>
        <el-form-item label="房间说明" :label-width="formLabelWidth">
          <el-input v-model="form.typeExplain" autocomplete="off" placeholder="请输入说明" type="number"
            value="this.form.discountRate"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="this.status === 0">确 定</el-button>
        <el-button type=" primary" @click="updateType" v-else>更 新</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateRoomType, createRoomType, deleteRoomType, getAllRoomTypeList } from '@/api/room'

export default {
  name: 'MemberManage',
  data() {
    return {
      currentPage: 1,
      total: 0,
      pageSizes: [],
      pageSize: 5,
      roomTypeList: [],
      dialogFormVisible: false,
      form: {
        roomType: '',
        price: '',
        description: '',
        roomUrl: '',
        isAdvice: '',
        typeExplain: ''
      },
      formLabelWidth: '120px',
      status: 0,
    }
  },
  watch: {
    dialogFormVisible(val) {
      if (val === false) {
        this.status = 0
      }
    }
  },
  created() {
    for (let i = 0; i < 5; i++) {
      this.pageSizes.push(5 * (i + 1))
    }
    this.fetchData()
  },
  methods: {
    fetchData() {
      getAllRoomTypeList().then(res => {
        this.roomTypeList = res
        this.total = this.roomTypeList.length
        for (let i = 0; i < this.roomTypeList.length; i++) {
          this.roomTypeList[i].isAdvice = this.roomTypeList[i].isAdvice === 1 ? '推荐' : '不推荐'
        }
      })
    },
    add() {
      if (this.form.vipType === '' || this.form.price === '' || this.form.discountRate === '') {
        this.$message('内容没写全')
      } else {
        this.form.isAdvice = this.form.isAdvice === '推荐' ? 1 : 0
        createRoomType(this.form).then(res => {
          this.dialogFormVisible = false
          this.fetchData()
          this.$notify({
            title: '成功',
            message: '会员类型创建成功',
            type: 'success'
          })
          this.form = {}
        })
      }
    },
    deleteType(id) {
      this.$confirm('此操作将永久删除该房间类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.warn(id)
        deleteRoomType(id).then(res => {
          this.fetchData()
          this.currentPage = 1
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editType(id) {
      console.warn(this.roomTypeList)
      this.roomTypeList.forEach(item => {
        if (item.roomTypeId === id) {
          this.form = item
        }
      })
      this.status = id
      this.dialogFormVisible = true
    },
    updateType() {
      console.log("11321")
      // console.log(this.form)
      this.form.roomTypeId = this.status
      this.form.isAdvice = this.form.isAdvice === '推荐' ? 1 : 0
      updateRoomType(this.form).then(res => {
        this.$message({
          type: 'success',
          message: '更新成功!'
        })
        this.status = 0
        this.dialogFormVisible = false
        this.fetchData()
      }).catch(err => {
        this.$message({
          showClose: true,
          message: '更新失败' + err.message,
          type: 'error'
        });
      })
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'vipType') {
        switch (row.vipType) {
          case '黄金':
            return { color: '#FFD700', fontWeight: 'bold' }; // 金色
          case '白银':
            return { color: '#C0C0C0', fontWeight: 'bold' }; // 银色
          case '青铜':
            return { color: '#CD7F32', fontWeight: 'bold' }; // 铜色
          case '铂金':
            return { color: '#008899', fontWeight: 'bold' }; // 铂金色
          case '钻石':
            return { color: '#0094c8', fontWeight: 'bold' }; // 钻石色
          default:
            return {};
        }
      }
      return {};
    },
    //切换每页条数容量
    handleSizeChange(val) {
      this.currentPage = 1
      this.pageSize = val
      this.fetchData()
    },
    //切换当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    indexMethod(index) {
      return index + 1 + (this.currentPage - 1) * this.pageSize;
    },
  }
}
</script>

<style lang="less" scoped>
.custom-manage {
  display: flex;
  justify-content: center;
}

.el-card {
  display: flex;
  flex-direction: column;
  width: 94%;
  margin: 20px 0;

  h3 {
    position: relative;
    height: 35px;

    .el-button {
      position: absolute;
      right: 10px;
      top: 5;
    }
  }

  .el-table {
    width: 100%;
    text-align: center;

    .el-table-column {
      text-align: center;
    }
  }
}

.el-pagination {
  margin: 10px 0 10px 300px;
}
</style>
